/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package ui.windows.components.panels.serverInformationPanel;

import processing.objects.Server;
import utils.properties.Configuration;
import utils.tns.GetTNS;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Handles state changes on panel's TNS list
 *
 * @author rymek
 *         User: rymek
 *         Date: 19.07.11
 *         Time: 09:02
 */
class ServerListItemListener implements ItemListener {

    private final ServerInformationPanel serverInformationPanel;      // parent ServerInformationPanel

    /**
     * Constructor.
     * Creates reference to parent ServerInformationPanel
     *
     * @param serverInformationPanel parent panel
     */
    public ServerListItemListener(ServerInformationPanel serverInformationPanel) {
        this.serverInformationPanel = serverInformationPanel;
    }

    /**
     * Handles state changes in TNS dropdown to populate
     * server information fields
     *
     * @param e event
     */
    @Override
    public void itemStateChanged(ItemEvent e) {

        if (this.serverInformationPanel.getTnsList().getSelectedItem().toString().equals("")) {

            this.serverInformationPanel.getServerIpAddress().setText("");
            this.serverInformationPanel.getServerPort().setText("");
            this.serverInformationPanel.getServerUsername().setText("");
            this.serverInformationPanel.getServerUsername().setEnabled(false);
            this.serverInformationPanel.getServerPassword().setText("");
            this.serverInformationPanel.getServerPassword().setEnabled(false);

        } else {

            Server server = GetTNS.getMapDb().get(this.serverInformationPanel.getTnsList().getSelectedItem().toString());
            this.serverInformationPanel.getServerIpAddress().setText(server.getUrl());
            this.serverInformationPanel.getServerPort().setText(server.getPort());
            this.serverInformationPanel.getServerUsername().setEnabled(true);
            this.serverInformationPanel.getServerPassword().setEnabled(true);
            this.serverInformationPanel.getServerUsername().setText(Configuration.serverProperties.getUsername(server));
            this.serverInformationPanel.getServerPassword().setText(Configuration.serverProperties.getPassword(server));
        }
    }
}
